<?php


namespace app\admin\controller;

ini_set('memory_limit', '1024M');
use app\admin\Base\BaseController;
use app\commom\BaiDuApi;
use Fukuball\Jieba\Finalseg;
use Fukuball\Jieba\Jieba;
use Fukuball\Jieba\JiebaAnalyse;
use think\facade\Db;
use think\facade\View;
use think\facade\Request;
use app\admin\model\Article as ArticleModel;

class Article extends BaseController
{
    //文章列表页面
    public function index(){
        if (Request::isAjax()) {
            $list = ArticleModel::order('id','desc')->paginate(Request::get('limit'));
            $this->jsonApi('', 1, $list->items(), ['count' => $list->total(), 'limit' => Request::get('limit')]);
        }
        return View::fetch();
    }

    //文章删除
    public function del(){
        $article = ArticleModel::find(Request::post('id'));
        $status = $article->delete();

        if ($status){
            $this->jsonApi('删除成功',1);
        }

        $this->jsonApi('删除失败',0);
    }

    //文章批量删除
    public function delAll(){
        $ids = Request::post('ids');
        if (empty($ids)){
            $this->jsonApi('未选数据',0);
        }
        $idsArr = explode(',',$ids);
        $status = ArticleModel::destroy($idsArr);
        if ($status){
            $this->jsonApi('批量删除数据成功',1);
        }
        $this->jsonApi('批量删除数据失败',0);
    }

    //文章添加
    public function add(){
        if (Request::isAjax()){
            //获取请求参数
            $param = Request::param();
            //数据封装
            $articleData = array(
                'author_id' => 1,
                'article_author'=>$param['article_author'],
                'status'=>$param['status'],
                'article_title'=>$param['article_title'],
                'article_text'=>$param['article_text'],
                'article_content'=>$param['article_content'],
                'article_image_cover'=>$param['article_image_cover'],
                'article_images_count'=>1,
                'article_views'=>1,
                'article_recommend'=>1,
                'seo_keywords'=>$param['seo_keywords'],
                'seo_description'=>$param['seo_description'],
                'source'=>'原创',
            );
            // 启动事务
            Db::startTrans();
            try {
                //保存文章
                $articleId = Db::name('article')->insertGetId($articleData);
                //添加标签
                if (!empty($param['mytags'])){
                    //判断标签库是否存该标签,不存在则在标签库保存
                    foreach ($param['mytags'] as $tag){
                        $model = Db::name('tag')->where('tag_name', $tag)->findOrEmpty();
                        if (empty($model)){

                        }
                    }
                }
                // 提交事务
                Db::commit();
            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();
            }
            return $this->jsonApi('添加失败',0);
        }
        return View::fetch();
    }

    //文章编辑
    public function edit(){
    }

    //是否启用
    public function status(){
        $data = Request::post();
        $article = ArticleModel::update(['status' => $data['status'], 'id' => $data['id']]);
        if (empty($article)){
            $this->jsonApi("设置失败",0);
        }
        $this->jsonApi("修改成功",1);
    }

    //文章关键词分析
    public function articleAnalyseKeywords(){
        //获取分析内容
        $article_content = Request::param('article_content');
        Jieba::init();
        Finalseg::init();
        JiebaAnalyse::init();
        $keywords = JiebaAnalyse::extractTags($article_content, 10);
        return $this->jsonApi('',1,array_keys($keywords));
    }

    //文章标签分析
    public function articleAnalyseTags(){
        $article_content = Request::param('article_content');
        $article_title = Request::param('article_title');
        $tags = BaiDuApi::CreateTags($article_title,$article_content);
        $result = array();
        foreach ($tags['items'] as $item) {
            $tag = $item['tag'];
            $result[] = $tag;
        }
        return $this->jsonApi('',1,$result);

    }

    //文章摘要分析
    public function articleAnalyseAbstarct(){
        $article_content = Request::param('article_content');
        $description = BaiDuApi::getDescription($article_content, 120);
        return $this->jsonApi('',1,$description['summary']);
    }

}